<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
    List list = QBuilder.db("basedb").queryHql("from friend where status=" + STATUS_ENABLE + " and state = 1 and (dest_uid=" + token.getUid() + " or src_uid=" + token.getUid() + ")");
    if (isEmpty(list)) {
        msg = new Message(30041, "暂无好友");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<String> friendIds = new ArrayList<String>();
    for (int i= 0; i < list.size(); i++) {
        Map map = (Map)list.get(i);
        String destUid = Tools.getDesc(map.get("dest_uid"), token.getUid()+"");
        if (!destUid.equals(token.getUid()+"") && !friendIds.contains(destUid))
            friendIds.add(destUid);
        String srcUid = Tools.getDesc(map.get("src_uid"), token.getUid()+"");
        if (!srcUid.equals(token.getUid()+"") && !friendIds.contains(srcUid))
            friendIds.add(srcUid);
    }
    String uids = "-1";
    for (int i= 0; i < friendIds.size(); i++) {
        uids += "," + friendIds.get(i);
    }
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("status= ", STATUS_ENABLE).and("hospital=", hospital).and("SELF=", SELF).and("uid in ("+ uids +")")).query().getResultObj();
    List<User> users = QBuilder.db("basedb").select().from("User").where(new Where("status= ", STATUS_ENABLE).and("hospital=", hospital).and("id in ("+ uids +")")).query().getResultObj();
    List result = new ArrayList();
    for (int i = 0; i < users.size(); i++) {
        Map map = new HashMap();
        result.add(map);
        User user = users.get(i);
        String id = user.getId() + "";
        map.put("avatar", Tools.getDesc(user.getAvatar() + "", ""));
        map.put("user_name", Tools.getDesc(user.getUserName() + "", ""));
        map.put("nick_name", Tools.getDesc(user.getNickName() + "", ""));
        map.put("type", Tools.getDesc(user.getType() + "", ""));
        map.put("uid", id);
        for (int j = 0; j < userProfiles.size(); j++) {
            UserProfile userProfile = userProfiles.get(j);
            String uid = userProfile.getUid() + "";
            if (id.equals(uid)) {
                Long birth = Tools.getLong(userProfile.getBirthday() + "", -1L);
                map.put("birthday", birth < 0 ? "" : DateUtil.formatDate(new Date(birth)));
                map.put("gender", Tools.getDesc(userProfile.getGender() + "", ""));
                map.put("self", Tools.getDesc(userProfile.getSelf() + "", "0"));
                map.put("age", Tools.getDesc(userProfile.getAge() + "", ""));
                map.put("phone", Tools.getDesc(userProfile.getPhone() + "", ""));
                map.put("card_type", Tools.getDesc(userProfile.getCardType() + "", ""));
                map.put("medical_type", Tools.getDesc(userProfile.getMedicalType() + "", ""));
                map.put("name", Tools.getDesc(userProfile.getName() + "", ""));
                map.put("json", userProfile.getJson());
                userProfiles.remove(userProfile);
                break;
            }
        }
    }
    msg.put("data", result);
    Tools.encryptPrint(out, msg);
%>
